package vietnam.traveltools;

import java.sql.SQLException;

import vietnam.traveltools.variables.MySQLiteOpenHelper;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

public class FoodContentActivity extends Activity {
	private MySQLiteOpenHelper dataHelper;
	private static final String TAG = "FoodActivity";
	private TextView tvContent;
	private TextView tvName;
	private Cursor cursor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		
		setContentView(R.layout.content_activity);
		tvContent = (TextView) findViewById(R.id.tvContent);
		tvName = (TextView) findViewById(R.id.tvName);
		setTitle("Thông tin");

		Intent callerIntent = getIntent();
		Bundle packageFromCaller = callerIntent
				.getBundleExtra(MySQLiteOpenHelper.cl_id_food);
		int id = packageFromCaller.getInt(MySQLiteOpenHelper.cl_id_food);
		// Toast.makeText(getApplicationContext(), "Id là: " + id,
		// Toast.LENGTH_SHORT).show();

		try {
			dataHelper = new MySQLiteOpenHelper(FoodContentActivity.this);
			dataHelper.checkAndcopyDB();
			dataHelper.openDB();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}

		try {
			String sql = "SELECT * FROM " + MySQLiteOpenHelper.tb_name_food
					+ " WHERE " + MySQLiteOpenHelper.cl_id_food + " = " + id;

			Cursor cursor = dataHelper.SELECTSQL(sql);
			Log.d(TAG, "cursor: " + cursor);
			if (cursor != null) {
				if (cursor.moveToFirst()) {

					do {
						String get_cl_id_food = cursor.getString(cursor
								.getColumnIndex(MySQLiteOpenHelper.cl_id_food));

						String get_cl_name = cursor
								.getString(cursor
										.getColumnIndex(MySQLiteOpenHelper.cl_food_name));

						String get_cl_address = cursor
								.getString(cursor
										.getColumnIndex(MySQLiteOpenHelper.cl_food_address));

						String get_cl_city = cursor
								.getString(cursor
										.getColumnIndex(MySQLiteOpenHelper.cl_food_city));

						String get_cl_price = cursor
								.getString(cursor
										.getColumnIndex(MySQLiteOpenHelper.cl_food_price));

						if (get_cl_price == null) {
							get_cl_price = "Đang cập nhật";
						}
						String get_cl_type = cursor
								.getString(cursor
										.getColumnIndex(MySQLiteOpenHelper.cl_food_type));
						if (get_cl_type == null) {
							get_cl_type = "Đang cập nhật";
						}

						String get_cl_description = cursor
								.getString(cursor
										.getColumnIndex(MySQLiteOpenHelper.cl_food_description));

						String get_cl_time_job = cursor
								.getString(cursor
										.getColumnIndex(MySQLiteOpenHelper.cl_food_time_job));

						Log.d(TAG, "id_travel: " + get_cl_id_food);

						tvContent.setText(get_cl_description);
						tvName.setText(get_cl_name);
					} while (cursor.moveToNext());

				}
			}
		} catch (Exception e) {
			Toast.makeText(getApplication(), "Error: " + e, Toast.LENGTH_LONG)
					.show();
		}
	}

}
